package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;
    @Autowired
    BaseCategoryViewMapper baseCategoryViewMapper;
    @Resource
    BaseAttributeMapper baseAttributeMapper;


    @Override
    public List<JSONObject> getBaseCategoryList() {
        // 一级分类集合
        ArrayList<JSONObject> jsonObjects = new ArrayList<>();
        //查询所有一级分类
        List<BaseCategoryView> baseCategoryViews = this.baseCategoryViewMapper.selectList(null);
        if (CollectionUtils.isEmpty(baseCategoryViews)) {
            return null;

        }
        //根据一级id进行分组：一个一级分类就是一组
       Map<Long, List<BaseCategoryView>> category1Map = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

        category1Map.forEach((category1Id, categories2) -> {
            // 每一个kv对应一个一级分类
            JSONObject jsonObject1 = new JSONObject();
            jsonObjects.add(jsonObject1);
            // 一级分类id
            jsonObject1.put("categoryId", category1Id);
            // 一级分类 名称 ：直接从该分组的第一个数据获取
            jsonObject1.put("categoryName", categories2.get(0).getCategory1Name());


            // 一级分类下的二级分类：
            ArrayList<Object> jsonObjects2 = new ArrayList<>();
            jsonObject1.put("categoryChild", jsonObjects2);
            // 根据二级分级的ID 进行分组
            Map<Long, List<BaseCategoryView>> category2Map = categories2.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            category2Map.forEach((category2Id, categories3) -> {
                // 初始化二级分类ID
                JSONObject jsonObject2 = new JSONObject();
                jsonObjects2.add(jsonObject2);
                jsonObject2.put("categoryId", category2Id);
                jsonObject2.put("categoryName", categories3.get(0).getCategory2Name());


                // 三级分类的集合：直接把三级分类的集合转化成JsonOject集合
                jsonObject2.put("categoryChild", categories3.stream().map(category3 -> {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("categoryId", category3.getCategory3Id());
                    jsonObject.put("categoryName", category3.getCategory3Name());
                    return jsonObject;

                }).collect(Collectors.toList()));

            });


        });
        // 返回一级分类集合
        return jsonObjects;
    }

    @Override
    public List<BaseAttribute> findAttribute(Long categoryId1) {


        return this.baseAttributeMapper.findAttribute(categoryId1);
    }

    @Override
    public BaseCategoryView getBaseCategoryViewByCategory3Id(Long category3Id) {




        return  baseCategoryViewMapper.selectById(category3Id);
    }
}



